package com.weng.platform.draw.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.cloud.utils.CloudAuthUtil;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.draw.config.CoralConfig;
import com.weng.platform.draw.entity.DrawData;
import com.weng.platform.draw.model.request.DrawDataReq;
import com.weng.platform.draw.model.vo.DrawDataVo;
import com.weng.platform.draw.service.IDrawDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;


/**
 * 绘图信息 API实现
 *
 * @author weng
 * @date 2021-01-19 13:59:33
 *
 */
@Slf4j
@RestController
@RequestMapping("/draw-infos")
@Api(value = "绘图信息", tags = {"绘图信息 操作接口"})
public class DrawInfoApi extends BaseController {
		@Autowired
		private IDrawDataService drawDataService;

		/**
		 * 功能描述:单个获取绘图信息
		 */
		@ApiOperation(value = "获取绘图信息", notes = "")
		@GetMapping(value = "/{id}")
		public ResponseData get(@PathVariable("id") String id){
				DrawData drawData = drawDataService.getById(id);
				return success(drawData);
		}


		/**
		 * 条件 分页查询绘图信息
		 */
		@ApiOperation(value = "分页获取绘图信息", notes = "")
		@GetMapping(value = "")
		public ResponseData get(HttpServletRequest request, String name, Integer pageNo, Integer pageSize, String columnId, Boolean isQueryChild ) throws IOException {
				String ownerId = CloudAuthUtil.getCurUserId(request);
				IPage<DrawData> iPage = drawDataService.list(pageNo, pageSize, ownerId, name, columnId, isQueryChild);
				return success(iPage);
		}


		/**
		 * 新增绘图信息
		 */
		@ApiOperation(value = "新增绘图信息", notes = "")
		@PostMapping(value = "")
		public ResponseData add(@Valid @RequestBody DrawDataReq drawDataReq, HttpServletRequest request){
				DrawData drawData = initDrawData(request);
				drawData.setName(drawDataReq.getName());
				drawData.setSortNo(drawDataReq.getSortNo());
				drawData.setColumnId(drawDataReq.getColumnId());
				drawDataService.add(drawData);
				return ResponseData.success("保存成功");
		}

		private DrawData initDrawData(HttpServletRequest request){
				String userId = CloudAuthUtil.getCurUserId(request);
				String userName = CloudAuthUtil.getCurUserTrueName(request);
				DrawData drawData = new DrawData();
				drawData.setOwnerId(userId);
				drawData.setOwnerName(userName);
				drawData.setName(CoralConfig.DNAME);
				drawData.setBody(CoralConfig.DXML.getBytes());
				return drawData;
		}

		/**
		 * 更新绘图信息
		 */
		@ApiOperation(value = "更新绘图信息", notes = "")
		@PutMapping(value = "")
		public ResponseData update(@Valid @RequestBody DrawDataVo drawDataReq){
				DrawData drawData = Convert.convert(DrawData.class, drawDataReq);
				drawDataService.update(drawData);
				return success();
		}


		/**
		 * 删除绘图信息
		 */
		@ApiOperation(value = "ids 删除绘图信息", notes = "可以删除多个，中间以,号分隔")
		@DeleteMapping(value = "")
		public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
				String ids = deleteReq.getIds();
				if(StringUtils.isEmpty(ids)){
						return failed("删除失败,id不能为空！");
				}
				drawDataService.removeByIds(deleteReq.getIds());
				return success(deleteReq.getIds());
		}
}
